package com.stylefeng.guns.modular.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.stylefeng.guns.common.persistence.dao.TimerMapper;
import com.stylefeng.guns.common.persistence.model.Timer;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.util.ToolUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Date;
import java.util.List;

/**
 * 定时管理控制器
 *
 * @author fengshuonan
 * @Date 2018-01-24 16:40:02
 */
@Controller
@RequestMapping("/timer")
public class TimerController extends BaseController {

    private String PREFIX = "/system/timer/";

    @Autowired
    private TimerMapper timerMapper;

    /**
     * 跳转到定时管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "timer.html";
    }

    /**
     * 跳转到添加定时管理
     */
    @RequestMapping("/timer_add")
    public String timerAdd() {
        return PREFIX + "timer_add.html";
    }

    /**
     * 跳转到修改定时管理
     */
    @RequestMapping("/timer_update/{timerId}")
    public String timerUpdate(@PathVariable Integer timerId, Model model) {
        Timer timer = timerMapper.selectById(timerId);
        model.addAttribute(timer);
        return PREFIX + "timer_edit.html";
    }

    /**
     * 获取定时管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        if(ToolUtil.isNotEmpty(condition)){
            EntityWrapper<Timer> entityWrapper = new EntityWrapper<Timer>();
            Wrapper<Timer> name = entityWrapper.like("name","%"+condition+"%");
            List<Timer>  timers =timerMapper.selectList(name);
            return timers;
        }else{
            List<Timer>  timers =timerMapper.selectList(null);
            return timers;
        }
    }

    /**
     * 新增定时管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Timer timer) {
        if(timer!=null){
            timer.setCreatetime(new Date());
        }
        Integer  insert =  timerMapper.insert(timer);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除定时管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@Param("timerId")Integer timerId) {
        Integer delete = timerMapper.deleteById(timerId);
        return SUCCESS_TIP;
    }


    /**
     * 修改定时管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Timer timer) {
        if(timer!=null){
            timer.setCreatetime(new Date());
        }
        Integer update = timerMapper.updateAllColumnById(timer);
        return super.SUCCESS_TIP;
    }

    /**
     * 定时管理详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {

        return null;
    }
}